import pandas as pd
import numpy as np

##path 为数据集路径
def dataRead(path, x_data, y_data,res_json, data_size_begin, data_size_end):
    # 加载训练集
    train_data = pd.read_csv(path)
    num_of_instances = len(train_data)
    min_data = train_data.iloc[data_size_begin:data_size_end]
    pixels = min_data['pixels']
    emotions = min_data['emotion']
    print("数据集加载完成，数据集大小")
    print(len(pixels))

    # 表情类别数
    num_classes = 7
    import keras

    index=0;
    for emotion, img in zip(emotions, pixels):
        try:
            motion_int=int(emotion)
            res_json['num_samples'].append(motion_int)
            res_json['users'].append(index)
            emotion = keras.utils.to_categorical(emotion, num_classes)  # 独热向量编码
            val = img.split(" ")
            # pixels = np.array(val, 'float32')
            # x_val=pixels.reshape(-1, 48, 48, 1)
            x_val=[];
            for xval in val:
                x_val.append(int(xval))

            res_json['user_data'][index]={'x':x_val,'y':emotion.tolist()}
            x_data.append(pixels)
            y_data.append(motion_int)
            index=index+1
        except:
            index=index+1
            print("111")

    print("表情 分类完成 finish")
    print(len(x_data))

    # x_data = np.array(x_data)
    # y_data = np.array(y_data)
    # x_data = x_data.reshape(-1, 48, 48, 1)
    print("数据集 格式转换完成")
    print(len(res_json))
    # res = [];
    # res.append(x_data)
    # res.append(y_data)
    return res_json;